<?php

namespace App\Console\Commands;

use App\Models\Gift;
use App\Models\MoneyLog;
use App\Models\PointLog;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;

class UpdateLevel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'level:update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定时升级';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $zy_users = User::whereLevel(2)->get();

        /** @var \App\Models\User $user */

        foreach ($zy_users as $user) {
            $point = PointLog::query()->where('user_id', $user->id)->sum('point');
            if ($point > 100 * 10000) {
                $user->level++;
                $user->save();
            }
        }
        /**
         * 副理升经理
         */
        $fl_users = User::whereLevel(3)->get();
        foreach ($fl_users as $user) {
            $zy_count = User::where('level', '>', 1)->where('parent', $user->id)->count();
            if ($zy_count >= 3) {
                $children = User::whereParent('parent', $user->id)->get();
                $count = 0;
                foreach ($children as $child) {
                    $point = PointLog::query()->where('user_id', $child->id)->sum('point');
                    $count += $point;
                }
                if ($count + $user->point > 350 * 10000) {
                    $fl_count = User::where('level', '=', 3)->where('parent', $user->id)->count();
                    if ($fl_count > 2) {
                        $user->level = $user->level + 1;
                        $user->save();
                    }
                }
            }
        }
        /**
         * 经理升总监
         */
        $jl_users = User::whereLevel(4)->get();
        foreach ($jl_users as $user) {
            $jl_count = User::where('level', '>', 3)->where('parent', $user->id)->count();
            if ($jl_count >= 2) {
                $children = User::whereParent('parent', $user->id)->get();
                $count = 0;
                foreach ($children as $child) {
                    $point = PointLog::query()->where('user_id', $child->id)->sum('point');
                    $count += $point;
                }
                if ($count + $user->point > 700 * 10000) {
                    $user->level = $user->level + 1;
                    $user->save();
                }
            }
        }
        /**
         * 总监升区域
         */
        $jl_users = User::whereLevel(5)->get();
        $start_time = new Carbon('first day of last month');
        $end_time = new Carbon('last day of last month');
        $start_time = $start_time->toDateString();
        $end_time = $end_time->toDateString();
        foreach ($jl_users as $user) {
            $jl_count = User::where('level', '>', 4)->where('parent', $user->id)->count();
            if ($jl_count >= 2) {
                $children = User::whereParent('parent', $user->id)->get();
                $count = 0;
                foreach ($children as $child) {
                    $point = PointLog::query()->where('user_id', $child->id)->whereBetween('created_at', [$start_time, $end_time])->sum('point');
                    $count += $point;
                }
                $user_point =  PointLog::query()->where('user_id', $user->id)->whereBetween('created_at', [$start_time, $end_time])->sum('point');
                if ($count + $user_point > 700 * 10000) {
                    $user->level = $user->level + 1;
                    $user->save();
                }
            }
        }
    }

}
